package com.yunjian.core.admin;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunjian.common.utils.*;
import com.yunjian.core.entity.*;
import com.yunjian.core.service.ISysOrderDetailService;
import com.yunjian.core.service.ISysOrderService;
import com.yunjian.core.service.SysUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/sysOrder")
public class SysOrderController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private ISysOrderDetailService sysOrderDetailService;

    /**
     * 分页查询经销商订单列表
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        logger.info("分页查询经销商订单列表{}", JsonUtil.toJsonString(params));

        SysUserEntity user = HttpContextUtils.getLoginSysUserEntity();
        logger.info("当前登录用户{}", JsonUtil.toJsonString(user));
        SysUserRoleEntity role = sysUserRoleService.getOne(new QueryWrapper<SysUserRoleEntity>()
                .eq("user_id", user.getUserId()));
        int isAdmin = 0;
        if(role!=null && (role.getRoleId().intValue() == Constant.SUPER_ADMIN || role.getRoleId().intValue() == Constant.SUPER_DEALER)) {
            isAdmin = 1;
        }
        params.put("isAdmin", isAdmin);
        PageUtils page = sysOrderService.queryPage(params);

        return R.ok().put("page", page).put("isAdmin", isAdmin);
    }

    @PostMapping("/queryGoodsByPage")
    public R queryGoodsByPage(@RequestBody Map<String, Object> params){
        logger.info("分页查询商品列表{}", JsonUtil.toJsonString(params));
        PageUtils page = sysOrderService.queryGoodsByPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 生成经销商采购订单
     */
    @PostMapping("/createOrder")
    public R createOrder(@RequestBody Map<String, Object> params){
        logger.info("生成经销商采购订单{}", JsonUtil.toJsonString(params));
        sysOrderService.createOrder(params);
        return R.ok();
    }

    /**
     * 生成经销商采购订单
     */
    @PostMapping("/info")
    public R info(@RequestBody Map<String, Object> params){
        logger.info("查询经销商采购订单详情{}", JsonUtil.toJsonString(params));
        String id = StringUtil.obj2String(params.get("id"));

        SysOrder order = sysOrderService.getOne(new QueryWrapper<SysOrder>()
                .eq("id", Integer.parseInt(id)));

        List<SysOrderDetail> details = sysOrderDetailService.list(new QueryWrapper<SysOrderDetail>()
                .eq("order_no", order.getOrderNo()));

        return R.ok().put("order", order).put("details", details);
    }

    /**
     * 删除经销商采购订单
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Map<String, Object> params){
        logger.info("生成经销商采购订单{}", JsonUtil.toJsonString(params));
        String orderId = StringUtil.obj2String(params.get("orderId"));
        SysOrder sysOrder = sysOrderService.getOne(new QueryWrapper<SysOrder>()
                .eq("id", Integer.parseInt(orderId)));

        if(sysOrder.getStatus() == 0){
            sysOrderService.remove(new QueryWrapper<SysOrder>()
                    .eq("id", Integer.parseInt(orderId)));

            sysOrderDetailService.remove(new QueryWrapper<SysOrderDetail>()
                    .eq("order_no", sysOrder.getOrderNo()));

            return R.ok();
        }else{
            sysOrder.setDeleteFlag(0);
            sysOrderService.saveOrUpdate(sysOrder);
        }
        return R.ok();
    }

    /**
     * 修改经销商采购订单状态
     */
    @PostMapping("/updateOrderStatus")
    public R updateOrderStatus(@RequestBody Map<String, Object> params){
        logger.info("提交经销商采购订单{}", JsonUtil.toJsonString(params));
        String orderId = StringUtil.obj2String(params.get("orderId"));
        String status = StringUtil.obj2String(params.get("status"));
        SysOrder sysOrder = sysOrderService.getOne(new QueryWrapper<SysOrder>()
                .eq("id", Integer.parseInt(orderId)));

        sysOrder.setStatus(Integer.parseInt(status));
        sysOrderService.saveOrUpdate(sysOrder);
        return R.ok();
    }

    /**
     * 确认经销商采购订单
     */
    @PostMapping("/confirm")
    public R confirm(@RequestBody Map<String, Object> params){
        logger.info("确认经销商采购订单{}", JsonUtil.toJsonString(params));
        String orderId = StringUtil.obj2String(params.get("orderId"));
        SysOrder sysOrder = sysOrderService.getOne(new QueryWrapper<SysOrder>()
                .eq("id", Integer.parseInt(orderId)));

        sysOrder.setStatus(2);
        sysOrderService.saveOrUpdate(sysOrder);
        return R.ok();
    }

    /**
     * 发货经销商采购订单
     */
    @PostMapping("/ship")
    public R ship(@RequestBody Map<String, Object> params){
        logger.info("发货经销商采购订单{}", JsonUtil.toJsonString(params));
        String orderId = StringUtil.obj2String(params.get("orderId"));
        String shipCompany = StringUtil.obj2String(params.get("shipCompany"));
        String shipNo = StringUtil.obj2String(params.get("shipNo"));

        SysOrder sysOrder = sysOrderService.getOne(new QueryWrapper<SysOrder>()
                .eq("id", Integer.parseInt(orderId)));

        sysOrder.setStatus(3);
        sysOrder.setShipCompany(shipCompany);
        sysOrder.setShipNo(shipNo);
        sysOrder.setShipTime(new DateTime());
        sysOrderService.saveOrUpdate(sysOrder);
        return R.ok();
    }
}
